package controller

import (
	"ginWork/global"
	"ginWork/middleware"
	"ginWork/pkg"
	"ginWork/types"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"net/http"
	"path/filepath"
	"strconv"
	"time"
)

func GoodsTake(c *gin.Context) {
	file, _ := c.FormFile("filename")

	if file.Size > 1024*1024*10 {

		c.JSON(http.StatusOK, gin.H{
			"code": "-1",
			"msg":  "图片大小不能超过10M",
		})

		return
	}

	imageType := map[string]bool{
		".png":  true,
		".jpg":  true,
		".jpeg": true,
		".gif":  true,
		".webp": true,
	}

	ext := filepath.Ext(file.Filename)

	if !imageType[ext] {
		c.JSON(http.StatusOK, gin.H{"code": "-2", "msg": "文件格式错误"})

		return
	}

	format := time.Now().Format("2006-01-02")

	filename := format + file.Filename

	image := pkg.OssImage(file, filename)

	GoodsName := c.PostForm("GoodsName")
	GoodsPrice := c.PostForm("GoodsPrice")
	GoodsStock := c.PostForm("GoodsStock")
	GoodsIntroduce := c.PostForm("GoodsIntroduce")
	GoodsShing := c.PostForm("GoodsShing")
	GoodsStatus := c.PostForm("GoodsStatus")
	GoodsSort := c.PostForm("GoodsSort")

	price, _ := strconv.Atoi(GoodsPrice)
	shing, _ := strconv.Atoi(GoodsShing)
	status, _ := strconv.Atoi(GoodsStatus)
	sort, _ := strconv.Atoi(GoodsSort)
	stock, _ := strconv.Atoi(GoodsStock)

	if GoodsName == "" || GoodsIntroduce == "" {
		c.JSON(http.StatusOK, gin.H{"code": "-3", "msg": "不能为空"})

		return
	}

	if price == 0 || stock == 0 {
		c.JSON(http.StatusOK, gin.H{"code": "-4", "msg": "不能为0"})

		return
	}

	goods2 := types.Goods{
		GoodsName:      GoodsName,
		GoodsPrice:     price,
		GoodsStock:     stock,
		GoodsUrl:       image,
		GoodsIntroduce: GoodsIntroduce,
		GoodsShing:     shing,
		GoodsStatus:    status,
		GoodsSort:      sort,
	}

	global.Db.Take("goods").Create(&goods2)

	c.JSON(http.StatusOK, gin.H{"code": "0", "msg": "商品发布成功", "imageUrl": image, "filename": filename})

}

func GoodsList(c *gin.Context) {

	Page := c.PostForm("page")
	pageSize := 10

	page, _ := strconv.Atoi(Page)

	offset := (page - 1) * pageSize

	var goods []types.Goods

	global.Db.Table("goods").Limit(pageSize).Offset(offset).Find(&goods)

	c.JSON(http.StatusOK, gin.H{"code4": "0", "data": goods})

}

func GoodsOrder(c *gin.Context) {

	var goods []types.Goods

	global.Db.Table("goods").Limit(10).Order("goods_sort desc").Find(&goods)

	c.JSON(http.StatusOK, gin.H{"code4": "0", "order": goods})
}

func OrderType(c *gin.Context) {
	order := c.PostForm("orderType")

	orderType, _ := strconv.Atoi(order)

	var goods types.Goods

	orderList, err := goods.Order(orderType)
	if err != nil {
		return
	}

	c.JSON(http.StatusOK, gin.H{"code4": "0", "order": orderList, "orderType": orderType})
}

func GoodsFind(c *gin.Context) {
	Id := c.PostForm("id")

	gId, _ := strconv.Atoi(Id)

	goods := types.Goods{
		Model: gorm.Model{ID: uint(gId)},
	}

	global.Db.Table("goods").Where("id = ?", gId).First(&goods)

	c.JSON(http.StatusOK, gin.H{"code4": "0", "find": goods, "id": gId})

}

func GoodsTakeSho(c *gin.Context) {
	//鉴权
	//用户对商品进行收藏

	//创建一个对商品进行收藏的用户id  通孤傲鉴权后的token获取uID
	uId := middleware.MiddleWare()

	//查询在商品表是否存在此用户id

	var goods types.Goods

	//查询单个的详情数据
	global.Db.Table("goods").Where("id = ?", uId).First(goods)

	c.JSON(http.StatusOK, gin.H{"code": "0", "GoodsShing": goods.GoodsShing, "goods": goods})
}
